﻿using System.Collections.Generic;

namespace Fortune.POCO
{
    public class Result<T>
    {
        public string code { get; set; }
        public string message { get; set; }
        public string webSocketId { get; set; }
        public T data { get; set; }

        public bool isSuccess => code == "00000";
        public bool isFailed => !isSuccess;

        public Result(string webSocketId, T data) : this(CodeDesc.of(ResultCodeEnum.SUCCESS), webSocketId, data)
        {
        }
        
        public Result(CodeDesc codeDesc, string webSocketId, T data)
        {
            this.code = codeDesc.code;
            this.message = codeDesc.message;
            this.webSocketId = webSocketId;
            this.data = data;
        }

        public static Result<T> success(string webSocketId, T data)
        {
            return new Result<T>(webSocketId, data);
        }

        public static Result<T> failed(string webSocketId, T data)
        {
            return new Result<T>(CodeDesc.of(ResultCodeEnum.FAILED), webSocketId, data);
        }

    }

    public class CodeDesc
    {
        public string code { get; }
        public string message { get; }

        public CodeDesc(string code, string message)
        {
            this.code = code;
            this.message = message;
        }

        private static Dictionary<int, CodeDesc> codeMap;

        public static CodeDesc of(ResultCodeEnum resultCode)
        {
            if (codeMap == null)
            {
                init();
            }
            return codeMap?[(int)resultCode];
        }

        private static void init()
        {
            codeMap = new Dictionary<int, CodeDesc>()
            {
                [(int)ResultCodeEnum.SUCCESS] = new CodeDesc("00000", "一切OK"),
                [(int)ResultCodeEnum.FAILED] = new CodeDesc("A0001", "操作失败")
            };
        }
    }

    public enum ResultCodeEnum
    {
        SUCCESS,
        FAILED
    }
}